public without sharing class QuoteProductHandler {

    /*
    public static void bringPrice(List<QuoteProduct__c> qpList ){

        
        Set<Id> quoteSets  = new Set<Id>();

        Map<Id,Set<Id>>qDMap = new Map<Id,Set<Id>>();
        Map<Id,List<QuoteProduct__c>> qpMap =new Map<Id,List<QuoteProduct__c>>();
        Map<Id,Id > intents = new Map<Id,Id >();

        for(QuoteProduct__c p : qpList){

            intents.put(p.IntentProduct__c,null);
        }

        for(IntentProduct__c p : [select Id, Product__c from IntentProduct__c where Id in :intents.keySet() ]){
            
            intents.put(p.Id , p.Product__c);

        }

        for(QuoteProduct__c p : qpList){

            if(p.Product__c == null )p.Product__c = intents.get(p.IntentProduct__c );

            Set<Id> pids = qDMap.containsKey(p.Quote__c ) ? qDMap.get(p.Quote__c ) :  new Set<Id>();
            
            if(p.Product__c != null)pids.add(p.Product__c);

            pids.add(p.Product__c );

            qDMap.put(p.Quote__c , pids);

            List<QuoteProduct__c> qp  = qpMap.containsKey(p.Quote__c ) ? qpMap.get(p.Quote__c ) :  new List<QuoteProduct__c>();

            qp.add(p);
            qpMap.put(p.Quote__c , qp);

        }
        Map<Id,Quote> qMap = new Map<Id,Quote>();
        for(Quote q :[select Opportunity.Owner.SubCompany__c,Opportunity.Owner.Province__c,Id from Quote where Id in:qDMap.keySet()  and RecordType.DeveloperName = 'DealerQuote']){
            qMap.put(q.Id , q);
        }

        for(Id qId : qMap.keySet()){

            Set<Id> pids = qDMap.get(qId ) ;

            if(!qDMap.containsKey(qId))continue;

            String company = qMap.get(qId).Opportunity.Owner.SubCompany__c;
            String province = qMap.get(qId).Opportunity.Owner.Province__c;


            Map<Id,Price__c> priceMap = ImportObject.getPriceMap(pids, company,province);
            Map<Id,AveragePrice__c> aPriceMap = ImportObject.getLastMothPrice(pids,company);
            for(QuoteProduct__c qp:qpMap.get(qId )){

                qp.UnitPrice__c = priceMap.containsKey(qp.Product__c) ? priceMap.get(qp.Product__c).MarketPrice__c : null;
                qp.InstCost__c = priceMap.containsKey(qp.Product__c) ? priceMap.get(qp.Product__c).InstCost__c : null  ; 
                qp.AreaDealerPrice__c = priceMap.containsKey(qp.Product__c) ? priceMap.get(qp.Product__c).AreaDealerPrice__c : 0;
                qp.InvoiceDealerPrice__c = priceMap.containsKey(qp.Product__c) ? priceMap.get(qp.Product__c).InvoiceDealerPrice__c : 0;
                qp.HospitalManagerPrice__c = priceMap.containsKey(qp.Product__c) ? priceMap.get(qp.Product__c).HospitalManagerPrice__c : 0;
                qp.HospitalBottomPrice__c = priceMap.containsKey(qp.Product__c) ? priceMap.get(qp.Product__c).HospitalBottomPrice__c : 0;
                qp.CompanyBottomPrice__c = priceMap.containsKey(qp.Product__c) ? priceMap.get(qp.Product__c).CompanyBottomPrice__c : 0;
                qp.LastMonthAveragePrice__c = aPriceMap.containsKey(qp.Product__c) ? aPriceMap.get(qp.Product__c).AveragePrice__c : 0;
                qp.SpecialPrice__c = priceMap.containsKey(qp.Product__c) ? priceMap.get(qp.Product__c).SpecialPrice__c : 0 ; 
                
            }

        }

        
    }
    */
    public static void setOppProjectType(List<QuoteProduct__c> newList) {      
        
        Set<Id> oidSet = new Set<Id>();
        Set<Id> qtidSet = new Set<Id>();
        //Set<Id> newqtidSet = new Set<Id>();
        //Set<String> qpc1Set = new Set<String>();
        //取得报价产品相关业务机会ID
        for ( QuoteProduct__c qp : newList) {            
            qtidSet.add(qp.Quote__c);
        }
        //不为 开发区域经销商业务机会
        for ( Quote qt : [select id,OpportunityId,Status from Quote where Opportunity.recordtypeId !=:Utils.getRecordTypeIdByDevNameAndObjName('开发区域经销商业务机会','Opportunity')  and id in :qtidSet] ) {
            if(qt.Status != '草案中')oidSet.add(qt.OpportunityId);
        }  
        if(oidSet.size() > 0)OppSetProjectTypeHandler.calcOppSetProjectType(oidSet);
        /*
        //查询相关客户下的客户等级 
        Map<Id, Opportunity> oppMap = new  Map<Id, Opportunity>([SELECT Id,AccountId, Account.Level__c FROM Opportunity WHERE Id IN :oidSet  ]);
        
        //查询相关业务机会下最新的仪器报价
        for ( Quote qt : [ SELECT Id FROM Quote WHERE OpportunityID IN: oidSet AND Type__c = '仪器报价' Order By LastModifiedDate DESC limit 1 ] ) {
            newqtidSet.add(qt.Id);
        }
        //查询相关业务机会下最新的试剂报价
        for ( Quote qt : [ SELECT Id FROM Quote WHERE OpportunityID IN: oidSet AND Type__c = '试剂报价' Order By LastModifiedDate DESC limit 1 ] ) {
            newqtidSet.add(qt.Id);
        }
        
        Map<Id, List<QuoteProduct__c>> qpMap = new Map<Id, List<QuoteProduct__c>>();
        for ( QuoteProduct__c qp : [ SELECT Id, Product__r.Category1__c, Quote__r.OpportunityId FROM QuoteProduct__c WHERE 
                    Quote__c IN :newqtidSet ] ) {
            List<QuoteProduct__c> qplist =  new List<QuoteProduct__c>();
            Id oppid = qp.Quote__r.OpportunityId;           
            
            if ( qpMap.containsKey(oppid) == true ) {
                qplist = qpMap.get(oppid);   
            }
            qpList.add(qp);
            qpMap.put(oppid, qpList);
        }

        List<Opportunity> oppList = new List<Opportunity>();
        for ( Id oid : oidSet ) {
            Opportunity opp = new Opportunity(Id = oid);            
            String level = String.valueOf(oppMap.get(oid).Account.Level__c);
            
            List<QuoteProduct__c> qplist = qpMap.get(oid);            
            if(qplist != null){
                for (QuoteProduct__c qp : qplist ) {
                    qpc1Set.add(qp.Product__r.Category1__c);                    
                }
            }
            System.debug('xxxxxxxxxxxxxxxxxx:'+qpc1Set);
            opp.ProjectType__c  = IPQPMECPHandler.calcOppProjectType(level, qpc1Set);
            oppList.add(opp);
        }
        update oppList;   
        */         
           
    }
}